package com.wanxi.service.impl;

import com.wanxi.mapper.DataMapper;
import com.wanxi.mapper.SignMapper;
import com.wanxi.mapper.UserMapper;
import com.wanxi.model.Common;
import com.wanxi.model.Message;
import com.wanxi.model.Sign;
import com.wanxi.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SignServiceImpl implements SignService {
    @Autowired
    SignMapper signMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DataMapper dataMapper;
    @Override
    public Message<Sign> getSignRecord(Map map) {
        int page = Integer.parseInt((String) map.get("page"));
        int size = Integer.parseInt((String) map.get("size"));
        map.put("page",(page-1)* size);
        map.put("size",size);
        Message message = new Message();
        message.setList(signMapper.getSignRecord(map));
        message.setTotal(signMapper.getSignRecordCount(map));
        message.setModel(map);
        if (message.getList()!=null)
            message.setCode(200);
        else
            message.setCode(500);
        return message;
    }

    @Override
    public int addSignRecord(Sign sign) {
        return signMapper.addSignRecord(sign);
    }

    @Override
    public int addSignRecords(Map map) {
        String user_names = ((String) map.get("user_name")).replace("\"","");
        String status = ((String) map.get("status")).replace("\"","");
        String late_times = ((String) map.get("late_time"));
        String[] user_name = user_names.substring(1,user_names.length()-1).split(",");
        String[] statu = status.substring(1,status.length()-1).split(",");
        String[] late_time = late_times.substring(1,late_times.length()-1).split(",");
        List<Sign> list = new ArrayList<>();
        for(int i=0;i<user_name.length;i++){
            Sign sign = new Sign();
            sign.setUser(userMapper.getUserByUsername(user_name[i]));
            sign.setStatus(dataMapper.getSignStatusByCode(statu[i]));
            sign.setLateTime(late_time[i].replace("\"",""));
            list.add(sign);
        }
        System.out.println(list);
        int result = signMapper.addSignRecords(list);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public int deleteSigns(Map map) {
        String arrays = ((String) map.get("arrays")).replace("[","").replace("]","");
        String[] split = arrays.split(",");
        List list = Arrays.asList(split);
        int result = signMapper.deleteSigns(list);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public boolean getSignByUserAndSignTime(String username) {
        Long count = signMapper.getSignByUserAndSignTime(username);
        if (count>0)
            return false;
        else
            return true;
    }

}
